package com.qf.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qf.feign.CacheFeignClient;
import com.qf.pojo.Books;
import com.qf.service.IBooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/books")
public class BooksController {
    private CacheFeignClient cacheFeignClient;


    private IBooksService booksService;
    @Autowired
    public void setCacheFeignClient(CacheFeignClient cacheFeignClient) {
        this.cacheFeignClient = cacheFeignClient;
    }

    @Autowired
    public void setBooksService(IBooksService booksService) {
        this.booksService = booksService;
    }

    @GetMapping("/books")

    public List<Books> findAll() throws JsonProcessingException {
        System.out.println("来查书了");
      return   booksService.findAll();
    }

    @PostMapping("/testhset")
    public int testHset(String key,String field,String value){
        System.out.println(key+" "+field+" "+value);
        return cacheFeignClient.hSet(key,field,value);
    }


    @PostMapping("/testhmset")
    public int testHset(String key){

        Map hashMap=new HashMap();
        hashMap.put("name", "哎");
        hashMap.put("info", "閡");
        System.out.println("进来了");
        int i = cacheFeignClient.hMSet(key, hashMap);
        return i;
    }

    @GetMapping("/getall/{key}")
    public Map getAll(@PathVariable String key){
        Map map = cacheFeignClient.hGetAll(key);
        return map;
    }


    @GetMapping("/test2")
    @HystrixCommand(fallbackMethod = "test2fallback")
    public String test2() {
//        int i = 1 / 0;
        return "test2";
    }

    public String test2fallback() {
        System.err.println(Thread.currentThread().getName());
        return "test2fallback";
    }


    @GetMapping("/test3")
    public String test3(HttpServletRequest request) {
//        获取参数
        request.getParameterMap().forEach((key,value)->{
            System.err.println(key+"----3->"+ Arrays.toString(value));
        });
        return "test333";
    }

    @GetMapping("/test4")
    public String test4(HttpServletRequest request) {
        request.getParameterMap().forEach((key,value)->{
            System.err.println(key+"---4-->"+ Arrays.toString(value));
        });
        return "test444444";
    }


}
